Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CIP-0138 Builtin Array #6727

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

CIP-0138 Builtin Array #6727

wants to merge 8 commits into from

Conversation

Unisay
Copy link
Contributor

@Unisay Unisay commented Dec 2, 2024

Closes #6717

  • Built-in Array type itself, implemented by the Data.Vector.Strict
  • Builtin funtion LengthArray
  • Builtin funtion ListToArray
  • Tests

The PR is ready for review but build is not working until we update flake inputs (need vector 0.13.2)

@Unisay Unisay self-assigned this Dec 2, 2024
@Unisay Unisay force-pushed the yura/cip-0138-builtin-array branch from 92b8011 to 6de1458 Compare December 2, 2024 13:58
@Unisay Unisay force-pushed the yura/cip-0138-builtin-array branch from 6de1458 to acdd3ef Compare December 3, 2024 12:11
@Unisay Unisay force-pushed the yura/cip-0138-builtin-array branch from acdd3ef to cfd35ae Compare December 3, 2024 14:08
@Unisay Unisay force-pushed the yura/cip-0138-builtin-array branch from cfd35ae to 5de42d2 Compare December 3, 2024 15:11
@Unisay Unisay requested a review from a team December 5, 2024 14:35
@Unisay Unisay marked this pull request as ready for review December 5, 2024 14:35
Copy link
Contributor

@effectfully effectfully left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the lack of Plinth versions of the builtins intentional?

All looks great. Kinda unfortunate that more and more is needed to add a new builtin, maybe we could automate some more. Although seems not too bad still.

plutus-core/plutus-core/src/PlutusCore/Default/Builtins.hs Outdated Show resolved Hide resolved
let arrayOfInts = mkConstant @(Vector Integer) @DefaultUni () (Vector.fromList [1..10])
let term = apply () (tyInst () (builtin () ListToArray) integer) listOfInts
typecheckEvaluateCekNoEmit def defaultBuiltinCostModelForTesting term @?=
Right (EvaluationSuccess arrayOfInts)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some edge cases would be nice, like an empty array etc.

You should've probably used

evals
    :: DefaultUni `HasTermLevel` a
    => a
    -> DefaultFun
    -> [Type TyName DefaultUni ()]
    -> [Term TyName Name DefaultUni DefaultFun ()]
    -> TestNested

from the below, we use it for unit tests. Not important though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some edge cases would be nice, like an empty array etc.

Those will definitely get tested in the conformance tests later. Maybe it's OK to have some overlap here though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think it's better to have some overlap here, since it's cheap.

term = apply () (tyInst () (builtin () LengthArray) integer) arrayOfInts
typecheckEvaluateCekNoEmit def defaultBuiltinCostModelForTesting term @?=
Right (EvaluationSuccess expectedLength)
, testCase "indexArray" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also have some property tests, but feel free to add them in a follow-up.

plutus-tx/src/PlutusTx/Builtins/HasBuiltin.hs Show resolved Hide resolved
@Unisay
Copy link
Contributor Author

Unisay commented Dec 9, 2024

Is the lack of Plinth versions of the builtins intentional?

Yes.

@Unisay Unisay force-pushed the yura/cip-0138-builtin-array branch from 5de42d2 to 911f19d Compare December 9, 2024 14:15
@Unisay Unisay force-pushed the yura/cip-0138-builtin-array branch from 911f19d to 8535806 Compare December 12, 2024 09:49
@Unisay Unisay force-pushed the yura/cip-0138-builtin-array branch from 8535806 to d3942d7 Compare December 12, 2024 10:00
@Unisay Unisay force-pushed the yura/cip-0138-builtin-array branch from d3942d7 to 1c8d186 Compare December 13, 2024 10:42
@Unisay Unisay force-pushed the yura/cip-0138-builtin-array branch from 1c8d186 to fe5c0a3 Compare December 13, 2024 11:39
Copy link
Contributor

@kwxm kwxm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks OK modulo a few minor quibbles. I'd change the costs in Default.Builtins to be unimplementedCostingFunction just to make sure that we don't get any surprises before the costing's done properly though.

plutus-core/plutus-core/src/PlutusCore/Default/Builtins.hs Outdated Show resolved Hide resolved
DefaultUniArray _argUni -> pure $ Vector.length vec
_ -> throwing _StructuralUnliftingError "Expected an array but got something else"
{-# INLINE lengthArrayDenotation #-}
in makeBuiltinMeaning lengthArrayDenotation (runCostingFunOneArgument . paramLengthArray)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't mind it we said that new builtins must go last and move caseList and caseData there.

Yes, I think we should move them too.

{-# INLINE nullListDenotation #-}
in makeBuiltinMeaning
nullListDenotation
(runCostingFunOneArgument . paramNullList)

toBuiltinMeaning _semvar LengthArray =
let lengthArrayDenotation :: SomeConstant uni (Vector a) -> BuiltinResult Int
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe call this lengthOfArray for consistency with lengthOfByteString.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind, @effectfully are you ok with it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry, I think it was me who gave you the bad advice.

@@ -293,6 +305,7 @@ addConstantRose (CostRose cost1 forest1) (CostRose cost2 forest2) =
{-# INLINE addConstantRose #-}

instance ExMemoryUsage a => ExMemoryUsage [a] where
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing to do with this PR really, but I wonder if we can/should make the default memory usage for lists the one that returns the length of the list (ie, the one for the ListCostedByLength wrapper). That's a more natural measure of size for many applications (like listToArray) since in general you'll only be copying pointers rather than reallocating all of the contents of the list. I think this got added for serialiseData (because there you really do have to care about the contents) and it just ended up as the default. We'd have to check its uses carefully if we do want to change it though. Also ExMemoryUsage isn't really the best name in this context since it's really just a size measure.

@@ -62,3 +62,8 @@ instance PrettyDefaultBy config [a] => DefaultPrettyBy config (Set a) where
defaultPrettyBy config = prettyBy config . Set.toList
deriving via PrettyCommon (Set a)
instance PrettyDefaultBy config (Set a) => PrettyBy config (Set a)

instance PrettyDefaultBy config [a] => DefaultPrettyBy config (Vector a) where
defaultPrettyBy config = prettyBy config . toList
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if having identical textual representations could ever lead to ambiguity. I suppose in UPLC it'll always have list or array nearby anyway.

let arrayOfInts = mkConstant @(Vector Integer) @DefaultUni () (Vector.fromList [1..10])
let term = apply () (tyInst () (builtin () ListToArray) integer) listOfInts
typecheckEvaluateCekNoEmit def defaultBuiltinCostModelForTesting term @?=
Right (EvaluationSuccess arrayOfInts)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some edge cases would be nice, like an empty array etc.

Those will definitely get tested in the conformance tests later. Maybe it's OK to have some overlap here though.

@kwxm
Copy link
Contributor

kwxm commented Dec 31, 2024

I wonder if we have to worry about plutus-metatheory at this stage now. Because of the ceritfication stuff, plutus-executables now depends on plutus-metatheory and when you try to build that you get warnings about miissing cases for the new builtins. These used to be totally independent so you could get away with updating the metaheory in a separate PR, but maybe now there'll be CI problems because of the warnings. That'd be annoying.

Later: I think that it's OK. Even in master, plutus-metatheory doesn't know about caseList and caseData but it doesn't appear to cause any problems in CI.

@@ -91,6 +92,10 @@ instance HasFromBuiltin a => HasFromBuiltin (BuiltinList a) where
type FromBuiltin (BuiltinList a) = [FromBuiltin a]
fromBuiltin (BuiltinList xs) = map fromBuiltin xs

instance HasToBuiltin a => HasToBuiltin (Vector a) where
type ToBuiltin (Vector a) = BuiltinArray (ToBuiltin a)
toBuiltin = useToOpaque (BuiltinArray . map toBuiltin)
Copy link
Contributor

@kwxm kwxm Dec 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is causing a CI failure because BultinArray doesn't exist yet (in this PR). Can we just remove this from the PR?

Copy link
Contributor Author

@Unisay Unisay Jan 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, it fails atm. My idea was to first merge adjacent PR (related changes in Plinth) into this one first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement CIP-0138: Built-in Array data type
3 participants